from flask import Flask, request, jsonify  
from flask import Flask, render_template 
import requests
import json
  
app = Flask(__name__)  
  
@app.route('/')  
def home():  
    return render_template('index.html') 
   
@app.route('/s')
def search():
   query = request.args.get('query')  
   # age = request.args.get('age', type=int)  # 可以指定转换类型  
   # return f'{query}' 
   res = getMsg(query) #json
   return res, 200
  
def get_access_token():

   url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=urf17rTqw3L4Xtq5PQtb3fR6&client_secret=u6WyT9Omh0zOXYx4q85H7sCl8CZ7XGjU"

   payload = json.dumps("")
   headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
   }
   response = requests.request("POST", url, headers=headers, data=payload)
   return response.json().get("access_token")

def getMsg(query):
   url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + get_access_token()


   payload = json.dumps({
      "messages": [
         {
               "role": "user",
               "content": query
         }
      ]
   })
   headers = {
      'Content-Type': 'application/json'
   }

   response = requests.request("POST", url, headers=headers, data=payload)
   return response.content
   # print(response.text)
  
if __name__ == '__main__':  
    app.run(host='0.0.0.0', port=6767)
